var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var mph = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_asset:[],
    formNameV: '',
    showCZ: false,
    showCZ2: false,
    pay_sn:'',
    pay_type: 'wxpay_jsapi_xcx',
    pdrechargelist:[],
    predepositlog:[],
    chongzhibeln:true,
    showPayPwdInput: false,  //是否展示密码输入层
    pwdVal: '',  //输入的密码
    pwdVal1:'',
    payFocus: true, //文本框焦点
    logpage:1
  }, 
  xfbtn(){
    this.setData({
      chongzhibeln: true
    })
  },
  czbtn() {
    this.setData({
      chongzhibeln: false
    })
  },
  aaa() { },//取消冒泡
  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  // go_address_location(){
  //   wx.navigateTo({
  //     url: '../../pages/address_location/address_location'
  //   })
  // },
  formName: function (e) {
    this.setData({
      formNameV: e.detail.value
    })
  },
  tixianbtn1() {
    this.showInputLayer();
  },
  tixianbtn2() {
    this.setData({
      showCZ2: false
    })
     this.get_money_();
  },
  chongzhibtn1() {
    this.setData({
      showCZ: true
    })
  },
  chongzhibtn2() {
    this.setData({
      showCZ: false
    })
    this.add_money_cont();
  },
  hide_showCZ(){
    this.setData({
      showCZ: false
    })
  },
  /** 提现 */
  get_money_:function(){
    let url = 'index.php?act=other_member&op=pd_cash_add';
    var money = this.data.formNameV;
    var password = this.data.pwdVal1
    mph.postRequest(url, { pdc_amount: money, password: password}, function (res) {
      if (res.data.code == 200) {
        setTimeout(function () {
          mph.showSuccess(res.data.datas);
        }, 1000)
      }
    })
  },
  //会员-预存款存款
  //index.php?act=other_member&op=recharge_add&pdr_amount=2
  add_money_cont(){
    let url = 'index.php?act=other_member&op=recharge_add&pdr_amount=2';
    let that = this;
    let k = wx.getStorageSync('key');
    let formNameV = that.data.formNameV;
    let list = { act: 'other_member', op: 'recharge_add', pdr_amount: formNameV, key: k };
    console.log(url, list);
    if (formNameV) {
      console.log(url, list);
      mph.postRequest(url, list, function (res) {
        console.log('预存款存款:==>', res, formNameV);
        if (res.data.code == 200) {
          console.log('预存款存款:==>', res.data.datas);
          that.setData({
            pay_sn: res.data.datas.pay_sn
          })
          that.pay_new(res.data.datas.pay_sn);
        } else {
          mph.showErrorTost(res.msg);
        }
      })
    }else{
      console.log('请输入金额');
    }
  },
  /** 去支付
  */
  pay_new: function (pay_sn) {
    var that = this;
    var url = 'index.php?act=member_payment&op=pd_order';
    var data = {
      pay_sn: pay_sn,
      password: '',
      rcb_pay: 0,
      pd_pay: 0,
      payment_code: that.data.pay_type
    }
    
    mph.postRequest(url, data, function (e) {
      if (e.data.code == 200) {
        var data = e.data.datas;
        
        wx.requestPayment(
          {
            'timeStamp': data.timeStamp,
            'nonceStr': data.nonceStr,
            'package': data.package,
            'signType': 'MD5',
            'paySign': data.paySign,
            'success': function (res) {//支付成功后执行
              console.log(111, res)
              that.my_asset();
            },
            'fail': function (res) { console.log(222, res) },
            'complete': function (res) { console.log(333, res) }
          })

      }
    })
  },
  phone_bangding(){
    wx.navigateTo({
      url: 'pages/sele_phone/sele_phone'
    })
  },
  my_asset(){
    //获取用户的资金情况
    var url = 'index.php?act=member_index&op=my_asset';
    var that = this;
    mph.postRequest(url, {}, function (res) {
      console.log('我的商城:==>', res);
      if (res.data.code == 200) {
        that.setData({
          user_asset: res.data.datas
        })
      } else {
        mph.showErrorTost(res.msg);
      }
    })
  },
  pdrechargelist() {
    //存款充值明细
    var url = 'index.php?act=member_fund&op=pdrechargelist';
    var that = this;
    let k = wx.getStorageSync('key');
    mph.postRequest(url, {key:k}, function (res) {
      console.log('存款充值明细:==>', res);
      if (res.data.code == 200) {
        that.setData({
          pdrechargelist: res.data.datas.list
        })
      } else {
        mph.showErrorTost(res.msg);
      }
    })
  },
  predepositlog() {
    //预存款日志
    var that = this;
    let k = wx.getStorageSync('key');
    let page =  that.data.logpage;
    var url = 'index.php?act=member_fund&op=predepositlog2&key=${k}&curpage='+page+'&page=50';
    that.setData({
      logpage:page+1
    })
    
    mph.postRequest(url,{}, function (res) {
      console.log('预存款日志:==>', res);
      if (res.data.code == 200) {
        if (res.data.hasmore){
          var l = that.data.predepositlog.concat(res.data.datas.list)
          that.setData({
            predepositlog: l
          })
        }
      } else {
        mph.showErrorTost(res.msg);
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.predepositlog();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.my_asset();
    this.pdrechargelist();
    this.predepositlog();
    mph.is_yue_pass()
  },
  /**
 * 显示支付密码输入层
 */
  showInputLayer: function () {
    this.setData({ showPayPwdInput: true, payFocus: true });
  },
  /**
   * 隐藏支付密码输入层
   */
  hidePayLayer: function () {
    var val = this.data.pwdVal;
    this.setData({ showPayPwdInput: false, payFocus: false, pwdVal: '', pwdVal1: val});
  },
  /**
   * 获取焦点
   */
  getFocus: function () {
    this.setData({ payFocus: true });
  },
  /**
   * 输入密码监听
   */
  inputPwd: function (e) {
    var that = this
    this.setData({ pwdVal: e.detail.value });
    if (e.detail.value.length >= 6) {
      //执行密码验证
      var url = 'index.php?act=member_buy&op=check_password';
      mph.postRequest(url, { password: e.detail.value }, function (res) {
        if (res.data.code == 200) {
          that.hidePayLayer(); 
          that.setData({
            showCZ2: true
          })
        } else {
          mph.showErrorTost();
        }
      })
    }
  }
})